package com.xander.myblog.controller;

import com.xander.myblog.VO.OwnerVO;
import com.xander.myblog.bean.ResultBean;
import com.xander.myblog.entity.User;
import com.xander.myblog.service.UserService;
import com.xander.myblog.util.ShortUUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 叶振东
 * @date 2018-11-20
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("login")
    @ResponseBody
    public ResultBean login(User user, HttpSession session) {
        User getUser = userService.login(user);
        session.setAttribute("user", getUser);
        return new ResultBean();
    }

    @PostMapping("register")
    @ResponseBody
    public ResultBean register(HttpSession session, User user, String verCode) {
        String code = (String) session.getAttribute("captcha");
        this.userService.register(user, code, verCode);
        return new ResultBean<>();
    }

    @GetMapping("logout")
    @ResponseBody
    public ResultBean logout(HttpSession session) {
        session.invalidate();
        return new ResultBean();
    }

    @PostMapping("theme")
    @ResponseBody
    public ResultBean changeTheme(String primary, String accent, String layout, HttpSession session) {
        User user = (User) session.getAttribute("user");
        OwnerVO ownerVO = (OwnerVO) session.getAttribute("owner");
        if (!("").equals(primary)) {
            user.setThemePrimary(primary);
            ownerVO.setThemePrimary(primary);
        }
        if (!("").equals(accent)) {
            user.setThemeAccent(accent);
            ownerVO.setThemeAccent(accent);
        }
        if (!("").equals(layout)) {
            user.setThemeLayout(layout);
            ownerVO.setThemeLayout(layout);
        }
        userService.update(user);
        session.removeAttribute("user");
        session.removeAttribute("theme");
        session.setAttribute("user", user);
        session.setAttribute("owner", ownerVO);
        return new ResultBean();
    }

    @PostMapping("uploadavatar")
    @ResponseBody
    public ResultBean uploadAvatar(@RequestParam("avatar") MultipartFile file, HttpServletRequest request, HttpSession session) throws Exception {
        User user = (User) session.getAttribute("user");
        String originalFilename = file.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        String fileName = sdf.format(new Date()) + "_" + ShortUUIDUtil.getShortUUID() + "." + suffix;
        String destFileName = request.getServletContext().getRealPath("/WEB-INF/classes/static/static/img/avatar") + File.separator + fileName;
        File destFile = new File(destFileName);
        destFile.getParentFile().mkdirs();
        file.transferTo(destFile);
        String avatar = "/static/img/avatar/" + fileName;
        User updateUser = new User();
        updateUser.setUserId(user.getUserId());
        updateUser.setAvatar(avatar);
        userService.update(updateUser);
        user.setAvatar(avatar);
        session.removeAttribute("user");
        session.setAttribute("user", user);
        return new ResultBean<>(avatar);
    }

    @GetMapping("userinfo")
    public String userinfo() {
        return "user/userinfo";
    }
}
